﻿using System;
using System.Linq;
using System.ComponentModel.DataAnnotations;

namespace TaoNiuOA.Models
{
    /// <summary>
    /// 申请或审批
    /// 用于审批的,如请假申请,调工资申请
    /// </summary>
    public class Apply
    {
        [Key]
        public int ApplyID { get; set; }

        [Required]
        [Display(Name = "标题")]
        public string Title { get; set; }

        [Display(Name = "类型")]
        public int ApplyTypeID { get; set; }

        [Display(Name = "申请时间")]
        public DateTime ApplyTime { get; set; }

        [Required]
        [Display(Name = "内容")]
        [DataType(DataType.MultilineText)]
        [StringLength(1024, ErrorMessage = "请输入1000字以内的内容.")]
        public string Content { get; set; }

        [Display(Name = "开始时间")]
        public DateTime? StartTime { get; set; }

        [Display(Name = "结束时间")]
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 状态//0为拒绝,1为同意,2为审批中
        /// </summary>
        [Display(Name = "状态")]
        public int Status { get; set; }

        [Required]
        [Display(Name = "申请者")]
        public int UserID { get; set; }

        [Display(Name = "审批者")]
        public int? Approver { get; set; }

        DbEntity db = new DbEntity();

        /// <summary>
        /// 获取该申请的类型
        /// </summary>
        /// <returns></returns>
        public ApplyType GetApplyType()
        {
            var at = db.ApplyTypes.SingleOrDefault(n => n.ApplyTypeID == this.ApplyTypeID);
            if (at == null)
                at = new ApplyType();
            return at;
        }

        /// <summary>
        /// 申请者信息
        /// </summary>
        /// <returns></returns>
        public User GetUser()
        {
            var u = db.Users.SingleOrDefault(n => n.UserID == this.UserID);
            if (u == null)
                u = new User();
            return u;
        }

        /// <summary>
        /// 审批者信息
        /// </summary>
        /// <returns></returns>
        public User GetApprover()
        {
            var u = db.Users.SingleOrDefault(n => n.UserID == this.Approver);
            if (u == null)
                u = new User();
            return u;
        }
    }
}